package com.yiren.dbaa.module.practice_set.line.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.dbaa.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Describe
 *
 * @author WRX
 * @date 2023/1/9
 */
public class LineHomeAdapter extends BaseAdapter {

    private Context context;
    private List<LineInfoEntity> lineList;

    public int mSelect = 0;
    private boolean isPartner;//是否是合伙人

    public LineHomeAdapter(Context context, List<LineInfoEntity> lineList) {
        this.context = context;
        this.lineList = lineList;
        isPartner = LoadConfigApi.loadUserParamConfig(context).isPartner();
    }

    @Override
    public int getCount() {
        return lineList.size();
    }

    @Override
    public Object getItem(int position) {
        return lineList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            convertView = View.inflate(context, R.layout.item_line_data, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        LineInfoEntity line = lineList.get(position);
        holder.tvLineName.setText(line.getName());
        holder.tvLineType.setText(line.getLineType() == 1 ? "考试路段" : "训练路段");
        holder.tvLinePoints.setText("点位数：" + line.getPointNumber());
        if (isPartner) {  //合伙人视角
            holder.tvUpdateBtn.setVisibility(View.GONE);
            //可更新
            holder.tvUpdate.setVisibility(line.isUpdate() ? View.VISIBLE : View.GONE);
            //是否下载
            holder.tvManage.setText(line.isDownLoad() ? "管理" : "下载");
            //未发布的
            if (line.getLineVer() == 0) {
                holder.tvLineVersion.setText("未公开发布，暂无版本");
                holder.tvDelete.setVisibility(View.VISIBLE);
                holder.tvUseRange.setVisibility(View.GONE);
                holder.tvManage.setText("管理");
                holder.vBar.setVisibility(View.GONE);
            } else {
                holder.tvLineVersion.setText("线路版本：V" + lineList.get(position).getLineVer());
                holder.tvDelete.setVisibility(View.GONE);
                holder.tvUseRange.setVisibility(View.VISIBLE);
                holder.tvUseRange.setText("使用范围：" + (line.getUseRange() == 1 ? "个别用户使用" : "全部用户使用"));
                holder.vBar.setVisibility(View.VISIBLE);
            }
        } else {  //教练视角
            holder.tvUpdate.setVisibility(View.GONE);
            holder.tvUseRange.setVisibility(View.GONE);
            holder.vBar.setVisibility(View.GONE);
            //是否下载
            holder.tvManage.setText(line.isDownLoad() ? "编辑线路" : "下载");
            //未发布的
            if (line.getLineVer() == 0) {
                holder.tvLineVersion.setText("未公开发布，暂无版本");
                holder.tvDelete.setVisibility(View.VISIBLE);
                holder.tvUseRange.setVisibility(View.GONE);
                holder.tvUpdateBtn.setVisibility(View.GONE);
                holder.tvManage.setText("编辑线路");
            } else {
                holder.tvLineVersion.setText("线路版本：V" + line.getLineVer());
                holder.tvDelete.setVisibility(View.GONE);
                holder.tvUpdate.setVisibility(line.isUpdate() ? View.VISIBLE : View.GONE);
            }
        }
        //更新
        holder.tvUpdateBtn.setOnClickListener(v -> {
            if (mOnUpdateClickListener != null) {
                mOnUpdateClickListener.onUpdateClick(position);
            }
        });
        //删除
        holder.tvDelete.setOnClickListener(v -> {
            if (mOnDeleteClickListener != null) {
                mOnDeleteClickListener.onDeleteClick(position);
            }
        });
        holder.tvManage.setOnClickListener(v -> {
            if (mOnManageClickListener != null) {
                mOnManageClickListener.onManageClick(position);
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv_line_name)
        TextView tvLineName;
        @BindView(R.id.tv_update)
        TextView tvUpdate;
        @BindView(R.id.tv_line_version)
        TextView tvLineVersion;
        @BindView(R.id.tv_line_type)
        TextView tvLineType;
        @BindView(R.id.tv_line_points)
        TextView tvLinePoints;
        @BindView(R.id.v_bar)
        View vBar;
        @BindView(R.id.tv_use_range)
        TextView tvUseRange;
        @BindView(R.id.tv_update_btn)
        TextView tvUpdateBtn;
        @BindView(R.id.tv_delete)
        TextView tvDelete;
        @BindView(R.id.tv_manage)
        TextView tvManage;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    //更新点击回调
    interface OnUpdateClickListener {
        void onUpdateClick(int position);
    }

    OnUpdateClickListener mOnUpdateClickListener;

    public void setOnUpdateClickListener(OnUpdateClickListener onUpdateClickListener) {
        mOnUpdateClickListener = onUpdateClickListener;
    }

    //条目删除回调
    public interface OnDeleteClickListener {
        void onDeleteClick(int position);
    }

    private OnDeleteClickListener mOnDeleteClickListener;

    public void setOnDeleteClickListener(OnDeleteClickListener onDeleteClickListener) {
        mOnDeleteClickListener = onDeleteClickListener;
    }

    //管理点击回调
    public interface OnManageClickListener {
        void onManageClick(int position);
    }

    private OnManageClickListener mOnManageClickListener;

    public void setOnManageClickListener(OnManageClickListener onManageClickListener) {
        mOnManageClickListener = onManageClickListener;
    }

    public void changeSelected(int position) {
        //刷新方法
        if (position != mSelect) {
            mSelect = position;
            notifyDataSetChanged();
        }
    }

}